package com.gxa.utils;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;

public class ImageUpload {

    public static String upload(MultipartFile tupian) throws IOException {
//        String path = "http://121.5.226.127:8080/a/";
        String path = "http://43.142.113.212:8088/uploadfiles/";

//为上传到服务器的文件取名，使用UUID防止文件名重复
//        String filename= UUID.randomUUID().toString()+"-"+myfile.getOriginalFilename();
        String type = tupian.getOriginalFilename().substring(tupian.getOriginalFilename().lastIndexOf("."));
        String filename= UUID.randomUUID().toString()+type;

//使用Jersey客户端上传文件
            Client client = Client.create();
            WebResource webResource = client.resource(path +"/" + URLEncoder.encode(filename,"utf-8"));
            webResource.put(tupian.getBytes());
            System.out.println("上传成功");
            return path+filename;
    }
}
